package com.lanhe.verifyRec.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lanhe.common.utils.DateUtils;
import com.lanhe.verifyRec.domain.LhVerifyRec;
import com.lanhe.verifyRec.mapper.LhVerifyRecMapper;
import com.lanhe.verifyRec.service.ILhVerifyRecService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 验证码记录表(LhVerifyRec)表服务实现类
 *
 * @author zhanggl
 * @since 2023-12-05 11:04:40
 */
@Service
@Slf4j
public class LhVerifyRecServiceImpl extends ServiceImpl<LhVerifyRecMapper, LhVerifyRec> implements ILhVerifyRecService {

    @Autowired
    private LhVerifyRecMapper lhVerifyRecMapper;//订单

    public static final Long  FIVE_MINUTE= 5 * 1000 * 60L;

    /**
     * @Description 验证码记录表
     * @MethodName addLhVerifyRec
     * @Param [dto]
     * @ReturnType java.lang.Integer
     * @Date  2023/12/5 14:49
     * @Author zxx
     * @Since 1.0
     */
    @Override
    public Integer addLhVerifyRec(LhVerifyRec dto) throws Exception {
        log.info("LhVerifyRecServiceImpl--->addLhVerifyRec 添加验证码记录表传参：【{}】",JSON.toJSONString(dto));
        dto.setCreateTime(DateUtils.getNowDate());
        int num = lhVerifyRecMapper.insert(dto);
        log.info("LhVerifyRecServiceImpl--->addLhVerifyRec 添加验证码记录表 返回结果：【{}】",JSON.toJSONString(num));
        return num;
    }
    /**
     * @Description 验证验证码是否有效
     * @MethodName validate
     * @Param [code, verifyScene]
     * @ReturnType java.lang.Boolean
     * @Date  2023/12/5 14:49
     * @Author zxx
     * @Since 1.0
     */
    @Override
    public Boolean validate(String verifyAccount, String verifyCode, Integer verifyScene) {
        log.info("LhVerifyRecServiceImpl--->validate 验证验证码 入参：手机号【{}】,验证码【{}】，类型【{}】",JSON.toJSONString(verifyAccount),JSON.toJSONString(verifyScene),JSON.toJSONString(verifyScene));
        Boolean resResult= false;
        LhVerifyRec lhVerifyRec = this.getOne(new LambdaQueryWrapper<LhVerifyRec>()
                .eq(LhVerifyRec::getVerifyAccount, verifyAccount)
                .eq(LhVerifyRec::getVerifyCode, verifyCode)
                .eq(LhVerifyRec::getVerifyType, verifyScene)
                .orderBy(true, false, LhVerifyRec::getCreateTime)
                .last("limit 1"));
        if(ObjectUtil.isNull(lhVerifyRec) || ObjectUtil.isEmpty(lhVerifyRec)){
            return false;
        }
       if(DateUtils.getNowDate().getTime()-lhVerifyRec.getCreateTime().getTime()<FIVE_MINUTE){//5分钟内 返回成功
           resResult =  true;
       }
        log.info("LhVerifyRecServiceImpl--->validate 验证验证码 返回结果：【{}】",JSON.toJSONString(resResult));
        return resResult;
    }
}

